package ThirdPagh;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.components.Description;
import com.gyf.immersionbar.ImmersionBar;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import SHU.water2.R;
import SHU.water2.Spinnerstyle;

import static SHU.water2.loginin.connection;

public class waterLevelList extends AppCompatActivity implements View.OnClickListener {
    private Spinner spinner1;
    private Spinner spinner2;
    private Spinner spinner3;
    private Spinner spinner4;
    private Spinner spinner5;
    private Spinner spinner6;
    private ArrayAdapter<String> arr_adapter4;
    private ArrayAdapter<String> arr_adapter5;
    private ArrayAdapter<String> arr_adapter6;

    public boolean getdata = false;
    public List<HashMap<String, Object>> data;
    private Handler handler;
    private String[] town;
    private String[] point;
    private String[] site;
    private float outriver[];
    private float inriver[];
    private HashMap<String, Object> item;
    private String sql = "select DISTINCT location_zhen from pump_site";

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.waterlevel);
        ImmersionBar.with(this).statusBarColor(R.color.color).fitsSystemWindows(true).init();
        handler = new Handler() {
            //接收消息
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                int flag = (int) msg.obj;
                switch (flag) {
                    case 1:
                        spinerset();
                        break;
//                    case 2:
//                        arr_adapter6 = new Spinnerstyle(waterLevelList.this, site);
//                        spinner6.setAdapter(arr_adapter6);
//                        break;
                    case 3:
                        setview();
                        break;
                }

            }
        };


        connect(sql);

        click();

    }

    private void click() {
//        findViewById(R.id.re1).setOnClickListener(this);
        findViewById(R.id.ret1).setOnClickListener(this);
        findViewById(R.id.check1).setOnClickListener(this);
        findViewById(R.id.check2).setOnClickListener(this);
        spinner1 = findViewById(R.id.spinner1);
        spinner2 = findViewById(R.id.spinner2);
        spinner3 = findViewById(R.id.spinner3);
        spinner4 = findViewById(R.id.spinner4);
        spinner5 = findViewById(R.id.spinner5);
        spinner6 = findViewById(R.id.spinner6);
    }

    private void connect(String sql) {
        //新建线程连接数据库，防止主界面卡死
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Statement statement = connection.createStatement();
                    if (sql.equals("select DISTINCT location_zhen from pump_site")) {
                        ArrayList<String> L1 = new ArrayList<>();
                        ArrayList<String> L2 = new ArrayList<>();
                        // 创建用来执行sql语句的对象
                        String sql1 = "select DISTINCT location_zhen from pump_site";
                        String sql2 = "select DISTINCT location_point from pump_site";
                        // 执行sql查询语句并获取查询信息
                        ResultSet rSet1 = statement.executeQuery(sql1);
                        statement = connection.createStatement();
                        ResultSet rSet2 = statement.executeQuery(sql2);

                        while (rSet1.next()) {
                            L1.add(rSet1.getString("location_zhen"));
                        }
                        while (rSet2.next()) {
                            L2.add(rSet2.getString("location_point"));
                        }

                        town = new String[L1.size()];
                        point = new String[L2.size()];
                        for (int i = 0; i < L1.size(); i++) {
                            town[i] = (String) L1.get(i);
                        }
                        for (int i = 0; i < L2.size(); i++) {
                            point[i] = (String) L2.get(i);
                        }

                        Message msg = new Message();
                        msg.obj = 1;
                        handler.sendMessage(msg);
                    }

//                    if (sql.startsWith("select s", 0)) {
//                        ArrayList<String> L1 = new ArrayList<>();
//                        // 创建用来执行sql语句的对象
//                        String sql1 = sql;
//                        // 执行sql查询语句并获取查询信息
//                        ResultSet rSet1 = statement.executeQuery(sql1);
//
//                        while (rSet1.next()) {
//                            L1.add(rSet1.getString("site_name"));
//                        }
//
//                        site = new String[L1.size()];
//                        for (int i = 0; i < L1.size(); i++) {
//                            site[i] = (String) L1.get(i);
//                        }
//
//                        Message msg = new Message();
//                        msg.obj = 2;
//                        handler.sendMessage(msg);
//                    }

                    if (sql.startsWith("site_name=", 0)) {
                        ArrayList<String> L1 = new ArrayList<>();
                        ArrayList<String> L2 = new ArrayList<>();
                        ArrayList<String> L3 = new ArrayList<>();
                        ArrayList<String> L4 = new ArrayList<>();
                        // 创建用来执行sql语句的对象
                        //String sql1 = "select * from water_level1 where site_name = '" + sql.substring(10) + "'";
                        //ResultSet rSet1 = statement.executeQuery(sql1);
                        String sql1 = "select * from pump_site where location_point = '" + sql.substring(10) + "'";
                        //statement = connection.createStatement();
                        ResultSet rSet1 = statement.executeQuery(sql1);

                        while (rSet1.next()) {
                            L1.add(rSet1.getString("site_name"));
                            L2.add(rSet1.getString("status"));

                            String sql2 = "select * from water_level1 where site_name = '" + rSet1.getString("site_name") + "'";
                            statement = connection.createStatement();
                            ResultSet rSet2 = statement.executeQuery(sql2);
                            if (rSet2.first()) {
                                L3.add(rSet2.getString("out_level"));
                                L4.add(rSet2.getString("in_level"));
                            } else {
                                L3.add(null);
                                L4.add(null);

                            }

                        }


                        data = new ArrayList<HashMap<String, Object>>();

                        for (int i = 0; i < L1.size(); i++) {
                            item = new HashMap<String, Object>();
                            item.put("name", L1.get(i));
                            item.put("in_level", L3.get(i));
                            item.put("out_level", L4.get(i));
                            //item.put("location_xu", L3.get(0));

                            if (L2.get(i).equals("1")) {
                                item.put("status", "开");
                            } else if (L2.get(i).equals("2"))
                                item.put("status", "关");
                            else item.put("status", "掉线");

                            data.add(item);
                        }

                        Message msg = new Message();
                        msg.obj = 3;
                        handler.sendMessage(msg);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    public void setview() {

        //创建SimpleAdapter适配器将数据绑定到item显示控件上
        SimpleAdapter adapter = new SimpleAdapter(waterLevelList.this, data, R.layout.rainlevellist,
                new String[]{"name", "in_level", "out_level", "levels", "status"}, new int[]{R.id.name, R.id.in, R.id.out, R.id.levels, R.id.status});

        ListView listView1 = (ListView) findViewById(R.id.list1);
        //ListView listView2 = (ListView) findViewById(R.id.list2);
        //实现列表的显示
        listView1.setAdapter(adapter);
        //listView2.setAdapter(adapter);

    }


    public void spinerset() {
        ArrayAdapter<String> arr_adapter1;
        ArrayAdapter<String> arr_adapter2;
        ArrayAdapter<String> arr_adapter3;

        String[] items1 = getResources().getStringArray(R.array.year);
        String[] items2 = getResources().getStringArray(R.array.months);
        String[] items3 = getResources().getStringArray(R.array.days);
        String[] items6 = getResources().getStringArray(R.array.site);


        //适配器1
        arr_adapter1 = new Spinnerstyle(this, items1);
        //设置样式
        arr_adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner1.setAdapter(arr_adapter1);

        //适配器2
        arr_adapter2 = new Spinnerstyle(this, items2);
        //设置样式
        arr_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner2.setAdapter(arr_adapter2);

        //适配器3
        arr_adapter3 = new Spinnerstyle(this, items3);
        //设置样式
        arr_adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner3.setAdapter(arr_adapter3);

        //适配器3
        arr_adapter4 = new Spinnerstyle(this, town);
        //设置样式
        arr_adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner4.setAdapter(arr_adapter4);
        spinner4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //String spinnerS4 = arr_adapter4.getItem(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //适配器3
        arr_adapter5 = new Spinnerstyle(this, point);
        //设置样式
        arr_adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner5.setAdapter(arr_adapter5);
        spinner5.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //String spinnerS5 = arr_adapter5.getItem(position).toString();
                //sql = "select site_name from pump_site where location_point =  '" + spinnerS5 + "'";
                sql = "site_name=" + arr_adapter5.getItem(position).toString();
                //connect(sql);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //适配器3
        /*arr_adapter6 = new Spinnerstyle(this, items6);
        //设置样式
        arr_adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner6.setAdapter(arr_adapter6);
        spinner6.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sql = "site_name=" + arr_adapter6.getItem(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });*/
    }


    //按钮设置
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.re1:
//                //read();
//                break;

            case R.id.ret1:
                this.finish();
                break;

            case R.id.check1:
                connect(sql);
                break;

            case R.id.check2:
                this.finish();
                break;
        }
    }

    public void read() {
        if (getdata == false) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("尚未读回数据，请刷新重试").setPositiveButton("是", null).show();
        } else
            setview();
    }
}
